function parse(str) {
  let value;

  try {
    value = JSON.parse(str);
  } catch(e) {
    value = null;
  }

  return value;
}

function stringify(obj) {
  let value;

  try {
    value = JSON.stringify(obj);
  } catch(e) {
    value = null;
  }

  return value;
}

export default function createStorage(key) {
  function getItem() {
    let value = window.localStorage.getItem(key);
    if (value) {
      value = parse(value);
    }
    return value;
  }

  function setItem(value) {
    window.localStorage.setItem(key, stringify(value));
  }

  return {
    getItem,
    setItem
  };
}